package com.woniu.adminservice.service.impl;

import com.woniu.adminservice.constant.SysConstant;
import com.woniu.adminservice.dto.MenuDTO;
import com.woniu.adminservice.entity.Menus;
import com.woniu.adminservice.dao.MenusDao;
import com.woniu.adminservice.exception.MenuServiceException;
import com.woniu.adminservice.service.MenusService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("menusService")
public class MenusServiceImpl implements MenusService {
    @Resource
    private MenusDao menusDao;

    @Override
    public Menus queryById(Integer id) {
        return menusDao.queryById(id);
    }

    @Override
    public List<Menus> queryAllByLimit(int offset, int limit) {
        return menusDao.queryAllByLimit(offset, limit);
    }
   
     @Override
    public List<Menus> findAll() {
        return menusDao.findAll();
    }

    /**
     * 向数据库添加菜单信息
     * @param menus
     * @return
     */
    @Override
    public Menus insert(Menus menus) {
        //设置展开状态为不展开
        menus.setSpread("false");
        menusDao.insert(menus);
        return menus;
    }

    @Override
    public Menus update(Menus menus) {
        menusDao.update(menus);
        return queryById(menus.getId());
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:根据菜单id删除单信息
     * return:
     */
    @Override
    public void deleteById(Integer id) {
        //判断当前节点下是否存在子节点
        Menus menus = menusDao.selectMenuByParentId(id);
        if (menus!=null ){
            throw new MenuServiceException("删除失败!该节点下有子节点");
        }
        menusDao.deleteById(id);
    }

    /**
     * 获取左侧导航栏节点信息
     * @param roleId
     * @return
     */
    @Override
    public List<MenuDTO> findAllByRoles(List roleId) {
        //根据登录的用户查询其拥有的所有权限
        List<Menus> menus = menusDao.selectMenusByRoles(roleId);
        List<MenuDTO> result = new ArrayList();
        for (Menus menu : menus) {
            //父节点信息
            if(menu.getParentId().equals(SysConstant.MEUNS_ROOT_ID)){
                MenuDTO dto = new MenuDTO();
                dto.setTitle(menu.getTitle());
                dto.setHref(menu.getHref());
                dto.setIcon(menu.getIcon());
                dto.setSpread(menu.getSpread());
                //存储子节点的结合
                List childrens = new ArrayList();
                //查找当前节点下的子节点
                for (Menus childMenu : menus) {
                    if(childMenu.getParentId().equals(menu.getId())){
                        //封装子节点对象
                        MenuDTO child = new MenuDTO();
                        child.setSpread(childMenu.getSpread());
                        child.setIcon(childMenu.getIcon());
                        child.setHref(childMenu.getHref());
                        child.setTitle(childMenu.getTitle());
                        childrens.add(child);
                    }
                }
                dto.setChildren(childrens);
                result.add(dto);
            }
        }
        return result;
    }

    /**
     * 新增菜单时判断是否重复
     * @param menus
     * @return
     */
    @Override
    public boolean checkMenuName(Menus menus) {
        List<Menus> menusList =menusDao.selectMenusByTitle(menus.getTitle());
        if (menusList == null || menusList.size()==0){
            return false;
        }
        for (Menus menus1 : menusList) {
            if (menus1.getParentId().equals(menus.getParentId())){
                return true;
            }
        }
        return false;
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:编辑菜单时,判断当前节点下菜单是否有重复
     * return:
     */
    @Override
    public boolean checkMenu(Menus menus) {
        Menus menu = menusDao.selectMenusByTitleParentId(menus.getTitle(),menus.getParentId());
        if (menu==null){
            return false;
        }
        //判断前端传来的id 是否与数据库根据title中查出的id一致
        if(menus.getId().equals(menu.getId())){
            return false;
        }
        return true;
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:根据角色id  获取该角色的权限信息
     * return:
     */
    @Override
    public List<Menus> findCheckMenusByRoleId(Integer id) {
        //查询所有菜单数据
        List<Menus> allMenus = menusDao.findAll();
        //默认拥有的权限菜单数据
        List<Menus> menus = menusDao.selectMenusByRoleId(id);
        for (Menus m : allMenus) {
            for (Menus menu : menus) {
                if(m.getId().equals(menu.getId())){
                    m.setChecked(true);
                }
            }
        }
        return allMenus;
    }


}